﻿using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Table;

namespace Services
{
    public class Meter_DeviceDataServices : BaseServices<DeviceData>, IMeter_DeviceDataServices
    {
        private readonly IMeter_DeviceDataRepository _repository;
        private readonly SqlSugarClient _client;

        public Meter_DeviceDataServices(SqlSugarClient client, IMeter_DeviceDataRepository repository) : base(repository)
        {
            _repository = repository;
            _client = client;
        }

        //设备状态看板（正常、报警、故障）
        public string PageList(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }
            var query = _client.Queryable<DeviceWorkState, DeviceInfo, ClientInfo, AllLevelUserClientInfoView>
                ((s, c, u, w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                .Where((s, c, u, w) => w.UserId == userId)
                 .GroupBy((s, c, u) => u.ClientId)
                 .Select((s, c, u) => new
                 {
                     ClientId = u.ClientId.ToString(),
                     ClientArea = SqlFunc.AggregateMax(u.ClientArea),
                     ClientName = SqlFunc.AggregateMax(u.ClientName),
                     CircuitName = SqlFunc.AggregateMax(u.CircuitName),
                     CreateDate = SqlFunc.AggregateMax(u.CreateDate),
                     Longtitude = SqlFunc.AggregateMax(u.Longtitude),
                     Latitude = SqlFunc.AggregateMax(u.Latitude),
                     //正常
                     Normal = SqlFunc.AggregateSum(SqlFunc.MappingColumn(s.DeviceStatus, "case DeviceStatus when '正常' then 1 else 0 end")),
                     //报警
                     Warning = SqlFunc.AggregateSum(SqlFunc.MappingColumn(s.DeviceStatus, "case DeviceStatus when '报警' then 1 else 0 end")),
                     //故障
                     Error = SqlFunc.AggregateSum(SqlFunc.MappingColumn(s.DeviceStatus, "case DeviceStatus when '故障' then 1 else 0 end"))
                 })
                 .MergeTable().Where((u) => u.ClientId != null);

            if (!bootstrap.search.IsEmpty())
            {
                query.Where((s) => s.ClientName.Contains(bootstrap.search));
            }

            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        public string MapList(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            var query = _client.Queryable<DeviceData, DeviceInfo, ClientInfo, AllLevelUserClientInfoView>
                ((s, c, u, w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                .Where((s, c, u, w) => w.UserId == userId)
                .GroupBy((s, c, u) => u.ClientId)
                .Select((s, c, u) => new
                {
                    ClientArea = SqlFunc.AggregateMax(u.ClientArea),
                    ClientName = SqlFunc.AggregateMax(u.ClientName),
                    Longtitude = SqlFunc.AggregateMax(u.Longtitude),
                    Latitude = SqlFunc.AggregateMax(u.Latitude),
                    
                    Pump1Run = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case when ParameterName like '%泵1运行%' then ParameterValue else '0' end")),
                    Pump1Alarm = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '%泵1故障%' then ParameterValue else '0' end")),
                    
                    Pump2Run = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%泵2运行%' then ParameterValue else '0' end")),
                    Pump2Alarm = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%泵2故障%' then ParameterValue else '0' end")),
                    
                    WaterLevel = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%液位%' then ParameterValue else '0' end")),
                    CH4 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%甲烷浓度%' then ParameterValue else '0' end")),
                    
                    Temperature = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%温度%' then ParameterValue else '0' end")),
                    Humidity = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%湿度%' then ParameterValue else '0' end")),
                    
                    WindDailyPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%风机日发电量%' then ParameterValue else '0' end")),
                    PVDailyPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case  when ParameterName like '%光伏日发电量%' then ParameterValue else '0' end")),

                }).ToList();

            return query.JilToJson();
        }
        //粗略地图,获取用户所在的区域
        public string RoughMapList(long userId)
        {
            var query = _client.Queryable<AllLevelUserClientInfoView>()
                .Where((s) => s.UserId == userId)
                 .Select((s) => new
                 {
                     DeptName = SqlFunc.AggregateMax(s.DeptName)
                 }).ToList();
            return query.JilToJson();
        }

        //公司下各设备当前实时数据
        public string MachineDetailsList(Bootstrap.BootstrapParams bootstrap, Int64 ClientId, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }
            var query = _client.Queryable<DeviceData, DeviceInfo, ClientInfo, AllLevelUserClientInfoView>
                ((s, c, u, w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                 .Where((s, c, u, w) => c.ClientId == ClientId && w.UserId == userId)
                 .GroupBy((s) => s.DeviceId)
                 .Select((s, c, u) => new
                 {
                     ClientId = SqlFunc.AggregateMax(u.ClientId),
                     ClientArea = SqlFunc.AggregateMax(u.ClientArea),
                     ClientName = SqlFunc.AggregateMax(u.ClientName),
                     CircuitName = SqlFunc.AggregateMax(u.CircuitName),
                     DeviceName = SqlFunc.AggregateMax(c.DeviceName),
                     DeviceId = SqlFunc.AggregateMax(s.DeviceId).ToString(),
                     CreateDate = SqlFunc.AggregateMax(u.CreateDate),
                     //温度
                     BatteryV = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '蓄电池电压' then ParameterValue else '0' end")),
                     WindV = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机电压' then ParameterValue else '0' end")),
                     //湿度
                     WindI = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机电流' then ParameterValue else '0' end")),
                     WindP = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机功率' then ParameterValue else '0' end")),
                     //液位
                     WindR = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机转速' then ParameterValue else '0' end")),
                     PVV = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '光伏电压' then ParameterValue else '0' end")),
                     //有毒有害
                     PVI = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '光伏电流' then ParameterValue else '0' end")),
                     PVP = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '光伏功率' then ParameterValue else '0' end")),
                     //摄像头截图
                     WindDailyPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机日发电量' then ParameterValue else '0' end")),
                     WindTotalPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '风机总发电量' then ParameterValue else '0' end")),

                     //温度
                     PVDailyPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '光伏日发电量' then ParameterValue else '0' end")),
                     PVTotalPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '光伏总发电量' then ParameterValue else '0' end")),
                     //湿度
                     DailyTotalPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '日总发电量' then ParameterValue else '0' end")),
                     TotalPower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '总发电量' then ParameterValue else '0' end")),
                     //液位
                     DailyUsePower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '日用电量' then ParameterValue else '0' end")),
                     TotalUsePower = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '总用电量' then ParameterValue else '0' end")),
                     //有毒有害
                     FloatV = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '浮充电压点' then ParameterValue else '0' end")),
                     MachineCommunicationH = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '设备唯一通信ID高位' then ParameterValue else '0' end")),
                     //摄像头截图
                     MachineCommunicationL = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '设备唯一通信ID低位' then ParameterValue else '0' end")),
                     ImageUrl = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '摄像头图像' then ParameterValue else '0' end")),

                     //温度
                     AllClose = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '整体关机' then ParameterValue else '0' end")),
                     Pump1Local = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵1本地' then ParameterValue else '0' end")),
                     //湿度
                     Pump2Local = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵2本地' then ParameterValue else '0' end")),
                     Pump1Remote = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵1远程' then ParameterValue else '0' end")),
                     //液位
                     Pump2Remote = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵2远程' then ParameterValue else '0' end")),
                     Pump1Alarm = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵1故障' then ParameterValue else '0' end")),
                     //有毒有害
                     Pump2Alarm = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵2故障' then ParameterValue else '0' end")),
                     Pump1Run = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵1运行' then ParameterValue else '0' end")),
                     //摄像头截图
                     Pump2Run = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '泵2运行' then ParameterValue else '0' end")),
                     MotorUp = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '支架电机上升' then ParameterValue else '0' end")),

                     //温度
                     MotorDown = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '支架电机下降' then ParameterValue else '0' end")),
                     WaterLevel = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '液位' then ParameterValue else '0' end")),
                     //湿度
                     CH4 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '甲烷浓度' then ParameterValue else '0' end")),
                     Temperature = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '温度' then ParameterValue else '0' end")),
                     //液位
                     Humidity = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '湿度' then ParameterValue else '0' end")),

                     LastTime = SqlFunc.AggregateMax(s.LastTime)
                 })
                 .MergeTable().Where((s) => s.DeviceId != null);

            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        //公司下各设备当前实时数据
        public string MachineDetailsTrendList(Bootstrap.BootstrapParams bootstrap, Int64 ClientId, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }
            var query = _client.Queryable<DeviceData, DeviceInfo, ClientInfo, AllLevelUserClientInfoView>
                ((s, c, u, w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                 .Where((s, c, u, w) => c.ClientId == ClientId && w.UserId == userId)
                 .GroupBy((s) => s.DeviceId)
                 .Select((s, c, u) => new
                 {
                     ClientId = SqlFunc.AggregateMax(u.ClientId),
                     ClientArea = SqlFunc.AggregateMax(u.ClientArea),
                     ClientName = SqlFunc.AggregateMax(u.ClientName),
                     CircuitName = SqlFunc.AggregateMax(u.CircuitName),
                     DeviceName = SqlFunc.AggregateMax(c.DeviceName),
                     DeviceId = SqlFunc.AggregateMax(s.DeviceId).ToString(),
                     CreateDate = SqlFunc.AggregateMax(u.CreateDate),
                     //温度
                     ParameterValue1 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '温度' then ParameterValue else '0' end")),
                     ParameterStatus1 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '温度' then ParameterStatus else '0' end")),
                     //湿度
                     ParameterValue2 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '湿度' then ParameterValue else '0' end")),
                     ParameterStatus2 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '湿度' then ParameterStatus else '0' end")),
                     //液位
                     ParameterValue3 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '液位' then ParameterValue else '0' end")),
                     ParameterStatus3 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '液位' then ParameterStatus else '0' end")),
                     //有毒有害
                     ParameterValue4 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '有毒有害' then ParameterValue else '0' end")),
                     ParameterStatus4 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '有毒有害' then ParameterStatus else '0' end")),
                     //摄像头截图
                     ParameterValue5 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '摄像头截图' then ParameterValue else '0' end")),
                     ParameterStatus5 = SqlFunc.AggregateMax(SqlFunc.MappingColumn(s.ParameterName, "case ParameterName when '摄像头截图' then ParameterStatus else '0' end")),

                     LastTime = SqlFunc.AggregateMax(s.LastTime)
                 })
                 .MergeTable().Where((s) => s.DeviceId != null);

            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }
    }
}
